
/*
This class draws a post-it in the selected screen.
You may put type as: DrawnPostIt.TODO, DrawnPostIt.DOING, DrawnPostIT.DONE, DrawnPostIt.IMPEDIMENT
*/
function DrawnPostIt( screen, x, y , width, onContainer ) {
    var self = this;
    var area = screen.appendElement( "<div enabled=\"true\" x=\""+x+"\" y=\""+y+"\" width=\""+width+"\" height=\""+(20+44)+"\"></div>" );
    var src;
    
    this.onContainer = onContainer; // Container
    
    this.frameTopLeft = area.appendElement( " <div x=\"0\" y=\"0\" width=\"9\" height=\"20\"></div> " );
    this.frameTopRight = area.appendElement( " <div x=\""+ (width-9) +"\" y=\"0\" width=\"9\" height=\"20\"></div> " );
    this.frameTop = area.appendElement( " <div x=\"9\" y=\"0\" width=\""+(width-9-9)+"\" height=\"20\"></div> " );
    
    src = "resources\\postIt\\postItAreaTextoLaterais.png";
    this.frameLeft = area.appendElement( " <div x=\"0\" y=\"19\" width=\"1\" height=\"45\" background=\""+src+"\"></div> " );
    this.frameRight = area.appendElement( " <div x=\""+(width-1)+"\" y=\"19\" width=\"1\" height=\"45\" background=\""+src+"\"></div> " );
    
    src = "resources\\postIt\\postItAreaDeTexto.png";
    this.textArea = area.appendElement( " <div x=\"1\" y=\"19\" width=\""+(width-1-1)+"\" height=\"45\" background=\""+src+"\"></div> " );
    
    this.star3 = area.appendElement( " <div enabled=\"true\" x=\""+(width-(10+16))+"\" y=\"2\" width=\"16\" height=\"15\"></div> " );
    this.star2 = area.appendElement( " <div enabled=\"true\" x=\""+(width-(10+16+16))+"\" y=\"2\" width=\"16\" height=\"15\"></div> " );
    this.star1 = area.appendElement( " <div enabled=\"true\" x=\""+(width-(10+16+16+16))+"\" y=\"2\" width=\"16\" height=\"15\"></div> " );
    
    this.titleLabel = area.appendElement(" <label x=\"10\" y=\"2\" width=\""+(width-(10+10+16+16+16))+"\" height=\"18\" color=\"#FFFFFF\" font=\"Tahoma\" size=\"8\">"+ "time left here" +"</label>");
    
    this.contentLabel = area.appendElement(" <label x=\"25\" y=\"20\" width=\""+(width-(25+1))+"\" height=\"42\" color=\"#000000\" wordwrap=\"true\" font=\"Tahoma\" size=\"8\">"+ "task here" +"</label>");
    
    
    src = "resources\\postIt\\botoes\\botaoEdit.png";
    //debug.info(" <div x=\""+(width-25-1)+"\" y=\""+(height-14-1)+"\" width=\"25\" height=\"14\" background=\""+src+"\"></div> ");
    this.buttonEdit = area.appendElement(" <div enabled=\"true\" x=\""+(width-25-1)+"\" y=\""+(20+44-14-1)+"\" width=\"25\" height=\"14\" background=\""+src+"\"></div> ");
    this.buttonEdit.onmouseover = function() {
        self.buttonEdit.background = "resources\\postIt\\botoes\\botaoEditPorCima.png";
    };
    this.buttonEdit.onmouseout = function() {
        self.buttonEdit.background = "resources\\postIt\\botoes\\botaoEdit.png";
    };
    this.buttonEdit.onclick = function() {
        showEditPost( self.postit, self.onContainer );
        //showLogin();
    };
    this.buttonEdit.tooltip = TOOLTIP_EDITPOST;
    
    
    this.button1 = area.appendElement("<div enabled=\"true\" x=\"5\" y=\"24\" width=\"19\" height=\"19\"></div>");
    this.button2 = area.appendElement("<div enabled=\"true\" x=\"5\" y=\"43\" width=\"19\" height=\"19\"></div>");    
    
    if (onContainer==TODO||onContainer==IMPEDED) {
        this.button1.background = "resources\\postIt\\botoes\\botaoBrancoDoing.png";
        this.button1.onmouseover = function() { self.button1.background = "resources\\postIt\\botoes\\botaoAzul.png"; };
        this.button1.onmouseup = function() { self.button1.background = "resources\\postIt\\botoes\\botaoAzul.png"; };
        this.button1.onmousedown = function() { self.button1.background = "resources\\postIt\\botoes\\botaoAzulApertado.png"; };
        this.button1.onmouseout = function() { self.button1.background = "resources\\postIt\\botoes\\botaoBrancoDoing.png"; };
        this.button1.onclick = function() {
            // TODO move this post-it to container Doing
            self.changeContainerDoing();
        };
        this.button1.tooltip = TOOLTIP_MOVETO + " " + SCRUM_DOING;
        
        this.button2.background = "resources\\postIt\\botoes\\botaoBrancoExcluir.png";
        this.button2.onmouseover = function() { self.button2.background = "resources\\postIt\\botoes\\botaoExcluir.png"; };
        this.button2.onmouseup = function() { self.button2.background = "resources\\postIt\\botoes\\botaoExcluir.png"; };
        this.button2.onmousedown = function() { self.button2.background = "resources\\postIt\\botoes\\botaoExcluirApertado.png"; };
        this.button2.onmouseout = function() { self.button2.background = "resources\\postIt\\botoes\\botaoBrancoExcluir.png"; };
        this.button2.onclick = function() {
            // TODO remove this post-it
            self.removePostIt();
        };
        this.button2.tooltip = TOOLTIP_DELETE;
    }
    else if (onContainer==DOING) {
        this.button1.background = "resources\\postIt\\botoes\\botaoBrancoDone.png";
        this.button1.onmouseover = function() { self.button1.background = "resources\\postIt\\botoes\\botaoVerde.png"; };
        this.button1.onmouseup = function() { self.button1.background = "resources\\postIt\\botoes\\botaoVerde.png"; };
        this.button1.onmousedown = function() { self.button1.background = "resources\\postIt\\botoes\\botaoVerdeApertado.png"; };
        this.button1.onmouseout = function() { self.button1.background = "resources\\postIt\\botoes\\botaoBrancoDone.png"; };
        this.button1.onclick = function() {
            // TODO move this post-it to container Done
            self.changeContainerDone();
        };
        this.button1.tooltip = TOOLTIP_MOVETO + " " + SCRUM_DONE;
        
        this.button2.background = "resources\\postIt\\botoes\\botaoBrancoImpedir.png";
        this.button2.onmouseover = function() { self.button2.background = "resources\\postIt\\botoes\\botaoImpedir.png"; };
        this.button2.onmouseup = function() { self.button2.background = "resources\\postIt\\botoes\\botaoImpedir.png"; };
        this.button2.onmousedown = function() { self.button2.background = "resources\\postIt\\botoes\\botaoImpedirApertado.png"; };
        this.button2.onmouseout = function() { self.button2.background = "resources\\postIt\\botoes\\botaoBrancoImpedir.png"; };
        this.button2.onclick = function() {
            // TODO move this post-it to container Impeded
            self.changeContainerImpeded();
        };
        this.button2.tooltip = TOOLTIP_MOVETO + " " + SCRUM_IMPEDED;
    }
    else if (onContainer==DONE) {
        this.button1.background = "resources\\postIt\\botoes\\botaoBrancoExcluir.png";
        this.button1.onmouseover = function() { self.button1.background = "resources\\postIt\\botoes\\botaoExcluir.png"; };
        this.button1.onmouseup = function() { self.button1.background = "resources\\postIt\\botoes\\botaoExcluir.png"; };
        this.button1.onmousedown = function() { self.button1.background = "resources\\postIt\\botoes\\botaoExcluirApertado.png"; };
        this.button1.onmouseout = function() { self.button1.background = "resources\\postIt\\botoes\\botaoBrancoExcluir.png"; };
        this.button1.onclick = function() {
            // TODO remove this post-it
            self.removePostIt();
        };
        this.button1.tooltip = TOOLTIP_DELETE;
    }
    
    this.area = area;
    this.area.visible = false;

    this.star1.onclick = function() {
        if (self.postit.getStars()==1){ self.setStars(0); }
        else { self.setStars(1); }
        user.getProject().alterPostIt( self.postit, self.onContainer );
    };
    this.star2.onclick = function() {
        if (self.postit.getStars()==2){ self.setStars(1); }
        else { self.setStars(2); }
        user.getProject().alterPostIt( self.postit, self.onContainer );
    };
    this.star3.onclick = function() {
        if (self.postit.getStars()==3){ self.setStars(2); }
        else { self.setStars(3); }
        user.getProject().alterPostIt( self.postit, self.onContainer );
    };
}

DrawnPostIt.prototype.STARRED = function() {
    return "resources\\postIt\\estrelinhaAtivada.png";
};
DrawnPostIt.prototype.UNSTARRED = function() {
    var ret = "resources\\postIt\\estrelinhaDesativada.png";
    if (this.layout) {
        if (this.layout=="yellow"||this.layout=="silver") {
            ret = "resources\\postIt\\estrelinhaDesativadaEscurinha.png";
        }
    }
    return ret;
};

DrawnPostIt.prototype.resize = function (y, width) {
    this.area.y = y;
    this.area.width = width;
    this.frameTopRight.x = (width-9);
    this.frameTop.width = (width-9-9);
    this.frameRight.x = (width-1);
    this.textArea.width = (width-1-1);
    this.star3.x = (width-10-16);
    this.star2.x = (width-10-16-16);
    this.star1.x = (width-10-16-16-16);
    this.titleLabel.width = (width-10-10-16-16-16);
    this.contentLabel.width = (width-25-1);
    this.buttonEdit.x = (width-25-1);
};

DrawnPostIt.prototype.setLayout = function ( layout ) {
    if (typeof layout == typeof "") {
        this.layout = layout;
        this.frameTopLeft.background = "resources\\postIt\\layout_" + layout + "\\postItCantoEsquerdo.png";
        this.frameTopRight.background = "resources\\postIt\\layout_" + layout + "\\postItCantoDireito.png";
        this.frameTop.background = "resources\\postIt\\layout_" + layout + "\\postItBarra.png";
        
        if (layout=="yellow" || layout=="silver") this.titleLabel.color="#000000";
        else this.titleLabel.color = "#FFFFFF";
        
    }
};

DrawnPostIt.prototype.setStars = function ( stars ) {
    if (typeof stars == typeof 0) {
        this.star1.background = this.UNSTARRED();
        this.star2.background = this.UNSTARRED();
        this.star3.background = this.UNSTARRED();
        if (stars >= 1) this.star1.background = this.STARRED();
        if (stars >= 2) this.star2.background = this.STARRED();
        if (stars >= 3) this.star3.background = this.STARRED();
        if (this.postit)
            this.postit.setStars( stars );
    }
};

DrawnPostIt.prototype.setPostIt = function ( postit ) {
    var stars = 0;
    var title = "";
    var task = "";
    var layout = "";
    
    if (typeof postit == typeof new PostIt()) {
        stars = postit.getStars();
        task = postit.getTask();
        if (postit.isFinished()) {
            layout = options("layoutTask");
            var daysFinalized = postit.daysFinalized();
            if (daysFinalized)
                title = daysFinalized + " " + SCRUM_DAYSAGO;
            else
                title = SCRUM_TODAY;
        }
        else {
            if (postit.expiresToday()) {
                title = SCRUM_TODAY;
                layout = options("layoutToday");
            }
            else {
                if (postit.isExpired()) {
                    title = postit.daysExpired() + " " + SCRUM_DAYSAGO;
                    layout = options("layoutExpired");
                }
                else {
                    layout = options("layoutTask");
                    if (postit.getExpirationDate()) {
                        title = postit.daysToExpire() + " " + SCRUM_DAYSLEFT;
                        if (postit.isNearExpiration()) {
                            layout = options("layoutNear");
                        }
                    }
                    else {
                        title = "";
                    }
                }
            }
        }
        this.postit = postit;
        this.setLayout(layout);
        this.area.visible = true;
    }
    else {
        this.postit = null;
        this.area.visible = false;
    }
    
    this.titleLabel.innerText = title;
    this.contentLabel.innerText = task;
    this.setStars(stars);
};

DrawnPostIt.prototype.changeContainerToDo = function() {
    //move this post-it to Todo
    user.getProject().alterContainer( this.postit, this.onContainer , TODO );
    updateGui( user.getProject() );
};

DrawnPostIt.prototype.changeContainerDoing = function() {
    //move this post-it to Doing
    user.getProject().alterContainer( this.postit, this.onContainer, DOING );
    updateGui( user.getProject() );
};

DrawnPostIt.prototype.changeContainerImpeded = function() {
    //move this post-it to Impeded
    user.getProject().alterContainer( this.postit, this.onContainer, IMPEDED );
    updateGui( user.getProject() );
};

DrawnPostIt.prototype.changeContainerDone = function() {
    //move this post-it to Done
    this.postit.finalize();
    user.getProject().alterContainer( this.postit, this.onContainer, DONE );
    updateGui( user.getProject() );
};

DrawnPostIt.prototype.removePostIt = function(  ) {
    user.getProject().removePostIt( this.postit, this.onContainer );
    user.getProject().sortAll();
    updateGui( user.getProject() );
};